<template>
  <div class='auto' id="editTree">
    <tree :model="model" v-ref:tree id='defaultTreeOpen'>
      <span partial>{{row.data.name}}
        <input type="checkbox" v-model="$parent.$parent.ids" :value="row.id" @click="$parent.$parent.modifyMark(row)">
      </span>
    </tree>
    <button type="button" name="button" class="btn btn-primary auto" @click="save()">保存</button>
  </div>
</template>

<script>
import * as Util from "../Util"
import co from "co"
import $ from 'jquery'

let saveGen = function * (self, saveData) {
  //yield self.$post('rs/license/save', {data:self.saveData})
  yield self.$http.post('rs/license/save', saveData)
  console.log("保存成功")
  yield self.$showMessage("保存成功")
  self.$back('refresh')
}
let getData = function * (self) {
  //let data = yield self.$post('rs/license/getData', {parentid:self.data.id},{resolveMsg: null, rejectMsg: null})
  let data = yield self.$http.post('rs/license/getData', {parentid:self.data.id},{resolveMsg: null, rejectMsg: null})
  console.log("获取到的数据为-->"+JSON.stringify(data))
  console.log("aaaaa")
  console.log("++"+data.bodyText+"++")
  console.log("bbbbb")
  console.log(data.bodyText.length)
  if(data.bodyText.length > 1) {
    data.body.forEach((value) => {
      let dataIds = value.byresourceresult.split(",")
      let res = Util.getResourceById(value.byresourceid)
      // 设置默认选中根
      self.roots.push(res)
      self.getIds(res, dataIds)
    })
  }
}

export default {
  title: "策略授权",
  props: [
    'data' // 被授权资源节点
  ],
  data () {
    return {
      ids: [], // 所有选中的节点id
      roots: [], // 选中的根节点，每个根节点产生一条授权规则
      dataIds: [],
      unselected: [],
      model: Util.f,
      str: '',
      saveData: {}
    }
  },
  ready () {
    // 查询当前data的权限
    this.getRightData()
  },
  methods: {
    // 获取权限数据
    getRightData() {
      let get = getData(this)
      return co(get)
    },
    modifyMark (row) {
      // console.log(row.mark)
      if(!this.ids.includes(row.id)) { // 选择
        this.modifyChlidrenTrue(row)
        this.modifyParentTrue(row)
      }else { // 取消选择
        this.modifyChlidrenFalse(row)
        this.modifyParentFalse(row)
      }
    },
    // 选择框被选择后，向下修改
    modifyChlidrenTrue (row) {
      // 修改本节点
      if (!this.ids.includes(row.id)) {
        this.ids.push(row.id)
      }
      // console.log(row)
      // 向下修改
      if (row.children && row.children.length > 0) {
        row.children.forEach((obj) => {
          this.modifyChlidrenTrue(obj)
        })
      }
    },
    // 向上修改
    modifyParentTrue(row) {
      if (!this.ids.includes(row.id)) {
        this.ids.push(row.id)
      }
      // 如果父节点没有在选中内容中，则自身为选中的根节点
      let parent = row.parent
      while (parent != null && !this.ids.includes(parent.id)) {
        parent = parent.parent
      }
      if (parent == null) {
        this.roots.push(row.data)
      }
    },
    // 取消选择，向下修改
    modifyChlidrenFalse (row) {
      // 修改本节点
      this.ids.$remove(row.id)
      // 向下修改
      if (row.children && row.children.length > 0) {
        row.children.forEach((obj) => {
          this.modifyChlidrenFalse(obj)
        })
      }
    },
    // 向上修改
    modifyParentFalse(row) {
      this.ids.$remove(row.id)
      // 如果父节点没有在选中内容中，则自身为选中的根节点
      let parent = row.parent
      while (parent != null && !this.ids.includes(parent.id)) {
        parent = parent.parent
      }
      // 从根中取消自身
      if (parent == null) {
        this.roots.$remove(row.data)
      }
    },
    // 保存策略授权
    save () {
      console.log("进入保存函数")
      // 设置保存父节点
      let saveData = {parentid: this.data.id, licenses:[]}
      // 对选中的每一个根节点，保存一条授权策略
      this.roots.forEach((value) => {
        let str = "给" + value.name + "授予不包含"
        let license = {}
        license.isinherit = "是"
        license.byisinherit = "是"
        license.resourceresult = ""
        license.parentid = this.data.id
        license.resourceid = this.data.id
        license.byresourceid = value.id
        license.righttype = ""
        license.licensepolicy = str + "的权限"
        // 获取不包含的权限
        this.unselected = []
        let unselected = this.make(value)
        license.byresourceresult = this.unselected.toString()
        saveData.licenses.push(license)
      })

      // 调用保存过程
      let gen = saveGen(this, saveData)
      return co(gen)
    },
    // 将未被选中的id反向选择出来
    make (data) {
      data.children.forEach((child) => {
        // 如果id被选择，继续向下查找，未被选择，将id添加到未被选择列中，不在进行循环
        if(this.ids.includes(child.id)){
          // this.str += child.name+","
          if (child.children && child.children.length > 0) {
            this.make(child)
          }
        }else {
          this.unselected.push(child.id)
          this.str += child.name+","
        }
      })
    },
    // 设置默认值时，从未被选择的字段中推断出被选择的项
    // child: 授权根
    // dataIds: 过滤项
    getIds (child, dataIds) {
      if (!dataIds.includes(child.id)) {
        this.ids.push(child.id)
      }
      child.children.forEach((row) => {
        this.getIds(row, dataIds)
      })
    }
    // defaultOpen (data, func) {
    //   data.forEach((row) => {
    //     if (row.children && row.children.length > 1) {
    //       this.defaultOpen(row.children, func)
    //     }
    //     func(row)
    //   })
    // }
  }
}
</script>
<style>
  .all-checked {
    vertical-align: text-top;
    margin-left: 10px !important;
  }
  td > .glyphicon {
    cursor: pointer;
  }
  th > input[type="checkbox"], td > input[type="checkbox"] {
    cursor: pointer;
  }
  #editTree div{
    margin-left: 0px;
    color: #000;
  }
  #editTree span {
    color: #000;
  }
  #editTree .list-group-item {
    background-color: #fff;
    border: none;
  }
  #editTree .list-group-item.active,
  #editTree  .list-group-item.active:hover,
  #editTree  .list-group-item.active:focus {
    background-color: #fff;
  }
</style>
